package com.hxd.otc.module.wallet.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerFragment;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.wallet.adapter.BodyPagerAdapter;
import com.hxd.otc.module.wallet.adapter.HeaderPagerAdapter;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.module.wallet.contract.NewWalletContract;
import com.hxd.otc.module.wallet.presenter.NewWalletPresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.StatusBarHelper;
import com.hxd.otc.widget.TitleBar;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by lzy on 2018/9/20 0020.
 * description ${钱包}
 */
public class NewWalletFragment extends LazyFragment<NewWalletPresenter> implements NewWalletContract.IView {
    @BindView(R.id.vp_header)
    ViewPager mVpHeader;
    @BindView(R.id.vp_body)
    ViewPager mVpBody;
    @BindView(R.id.title_bar)
    TitleBar mTitlebar;

    private ArrayList<BaseRecyclerFragment> fragments;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_wallet;
    }

    @Override
    protected NewWalletPresenter createPresenter() {
        return new NewWalletPresenter(this);
    }

    @Override
    protected void initView() {
    }

    WalletCurrencyFragment otcFragment;
    WalletCurrencyFragment normalFragment;

    @Override
    protected void initData() {
        fragments = new ArrayList<>();
        otcFragment = new WalletCurrencyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("pos", 0);
        otcFragment.setArguments(bundle);
        fragments.add(otcFragment);

        normalFragment = new WalletCurrencyFragment();
        Bundle noBundle = new Bundle();
        noBundle.putInt("pos", 1);
        normalFragment.setArguments(noBundle);
        fragments.add(normalFragment);

        mVpBody.setAdapter(new BodyPagerAdapter(getChildFragmentManager(), fragments));
        mVpHeader.setAdapter(new HeaderPagerAdapter());
        mVpHeader.setPageMargin(DensityUtil.dp2px(10));
        mVpHeader.setOffscreenPageLimit(2);
        mVpBody.setOffscreenPageLimit(2);
    }


    public void pageScrollToTop() {
        for (int i = 0; i < fragments.size(); i++) {
            ((CommonScrollInterface) fragments.get(i)).pageScrollToTop();
        }
    }

    public void pageScrollTo(int distance) {
        mVpHeader.setTranslationY(-distance);

        for (int i = 0; i < fragments.size(); i++) {
            if (i != getCurrPageIndex()) {
                ((CommonScrollInterface) fragments.get(i)).pageScrollTo(distance);
            }
        }
    }


    public int getCurrPageIndex() {
        return mVpBody.getCurrentItem();
    }

    @Override
    protected void initEvent() {
        mVpBody.addOnPageChangeListener(new BaseLinkPageChangeListener(mVpBody, mVpHeader) {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                pageScrollToTop();
            }
        });
        mVpHeader.addOnPageChangeListener(new BaseLinkPageChangeListener(mVpHeader, mVpBody) {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        });
    }


    ArrayList<WalletCurrencyBean> walletCurrencyBeans;

    public void setWalletCurrencyBeans(ArrayList<WalletCurrencyBean> walletCurrencyBeans) {
        this.walletCurrencyBeans = walletCurrencyBeans;
    }

    public ArrayList<WalletCurrencyBean> getNoramlWalletInfo() {

        return walletCurrencyBeans;
    }

    public void onNewIntent() {
        if (isAdded() && UserHelper.isLogin()) {
            for (Fragment fragment : getChildFragmentManager().getFragments()) {
                if (fragment instanceof WalletCurrencyFragment) {
                    ((WalletCurrencyFragment) fragment).onNewIntent();
                }
            }
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isFirstVisible && getUserVisibleHint()) {
            for (Fragment fragment : getChildFragmentManager().getFragments()) {
                if (fragment.isAdded() && fragment.getUserVisibleHint() && fragment instanceof WalletCurrencyFragment) {
                    ((WalletCurrencyFragment) fragment).lazyLoad();
                }
            }
        }
        super.lazyLoad();
    }
}
